package heven.holt.wan.android.loading.delegate

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.findNavController
import heven.holt.library.loading.LoadingStateView
import heven.holt.wan.android.R
import heven.holt.wan.android.databinding.HeadCommonBinding

class CommonTitleDelegate constructor(
    private val title: String = "",
    private val type: Int = 0,
    private val clickListener: View.OnClickListener? = null
) :
    LoadingStateView.ViewDelegate<LoadingStateView.ViewHolder>() {

    private val typeResourceMap: Map<Int, Int> = mapOf(
        TYPE_RANK_COIN to R.mipmap.ic_rank,
        TYPE_COIN_RULE to R.mipmap.ic_rule
    )

    companion object {
        const val TYPE_RANK_COIN = 1 //积分排行榜
        const val TYPE_COIN_RULE = 2 //积分规则
    }

    private lateinit var binding: HeadCommonBinding

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): LoadingStateView.ViewHolder {
        binding = HeadCommonBinding.inflate(inflater, parent, false)
        binding.black.setOnClickListener { view ->
            view.findNavController().navigateUp()
        }

        val rightResource = typeResourceMap[type] ?: 0
        if (rightResource > 0) {
            binding.rank.setImageResource(rightResource)
            binding.rank.setOnClickListener(clickListener)
        }

        return LoadingStateView.ViewHolder(binding.root)
    }

    override fun onBindViewHolder(holder: LoadingStateView.ViewHolder) {
        setTitle(title)
    }

    fun setTitle(title: String) {
        binding.title.text = title
    }
}